package com.hamilton.erik.pt.mobile.android.widget;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hamilton.erik.pt.core.model.EnclosureMaintenance;
import com.hamilton.erik.pt.mobile.android.R;

public class EnclosureMaintenanceListAdapter extends BaseAdapter {

	private List<EnclosureMaintenance> enclosureMaintenance;
	private LayoutInflater layoutInflater;
	
	public EnclosureMaintenanceListAdapter(Context context, List<EnclosureMaintenance> enclosureMaintenance) {
		this.enclosureMaintenance = enclosureMaintenance;
		layoutInflater = LayoutInflater.from(context);
	}
	
	@Override
	public int getCount() {
		return enclosureMaintenance.size();
	}

	@Override
	public Object getItem(int index) {
		return enclosureMaintenance.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		EnclosureMaintenanceHolder enclosureMaintenanceHolder;
		
		if(convertView == null) {
			convertView = layoutInflater.inflate(R.layout.enclosure_maintenance_row_view, null);
			
			enclosureMaintenanceHolder = new EnclosureMaintenanceHolder();
			
			enclosureMaintenanceHolder.action = (TextView)convertView.findViewById(R.id.enclosure_maintenace_action);
			enclosureMaintenanceHolder.time = (TextView)convertView.findViewById(R.id.enclosure_maintenace_time);
			
			convertView.setTag(enclosureMaintenanceHolder);
		}
		else {
			enclosureMaintenanceHolder = (EnclosureMaintenanceHolder)convertView.getTag();
		}
		
		enclosureMaintenanceHolder.action.setText(enclosureMaintenance.get(position).getAction().toString());
		enclosureMaintenanceHolder.time.setText(enclosureMaintenance.get(position).getTimestamp());
		
		return convertView;
	}
	
	static class EnclosureMaintenanceHolder {
		TextView action;
		TextView time;
	}

}
